package News.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class testservlet extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String text=(String)request.getParameter("text");
        System.out.println("text:"+text);

        String session=(String)request.getSession().getAttribute("name");
        System.out.println("session:"+session);

        ServletContext applocation=this.getServletContext();
        String a=(String)applocation.getAttribute("name");
        System.out.println("application:"+a);

        Cookie[] c1=request.getCookies();
        for(Cookie co:c1){
            String name=co.getName();
            if("name".equals(name)){
                System.out.println("Cookie:"+co.getValue());
            }
        }

        //转发sql
        Cookie c2=new Cookie("age","21");
        c2.setMaxAge(600);
        response.addCookie(c2);

        request.setAttribute("age",21);

        request.getSession().setAttribute("age",21);

        ServletContext cation=this.getServletContext();
        cation.setAttribute("age",21);

        request.getRequestDispatcher("test2021-12-09_forward.jsp").forward(request,response);

    }
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {

    }
}
